package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>  implements BrandService {

    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    public List<BrandDTO> queryBrandByCategory(Long id) {
        List<Brand> list = getBaseMapper().queryBrandByCategory(id);

        return BrandDTO.convertEntityList(list);
    }

    @Override
    public PageDTO<BrandDTO> queryByPaGe(String key, Integer page, Integer rows) {
       page=Math.min(page,100);
       rows=Math.max(rows,5);
        Page<Brand>info=new Page<>(page,rows);
        boolean isKeyExists = StringUtils.isNoneBlank(key);
            query().like(isKeyExists,"name",key).or().eq(isKeyExists,"letter",key).page(info);
            List<Brand> list = info.getRecords();
        return new PageDTO<>(info.getTotal(), info.getPages(), BrandDTO.convertEntityList(list));

    }

    //添加
    @Override
    @Transactional
    public void add(BrandDTO brandDTO) {
        //Dto转换为po
        Brand brand = brandDTO.toEntity(Brand.class);
        //添加品牌
        boolean save = save(brand);
        if (!save){
            throw new LyException(500,"添加失败");
        }
        //添加中间表关系
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(collect);

    }

    //删除
    @Override
    @Transactional
    public void deleteById(Long id) {
        boolean b = removeById(id);
        if (!b){
            throw  new LyException(500,"删除失败");
        }
        categoryBrandService.remove(
                new QueryWrapper<CategoryBrand>().eq("brand_id", id));

    }

    //更新
    @Override
    public void updateBrand(BrandDTO brandDTO) {
        //类型转换
        Brand brand = brandDTO.toEntity(Brand.class);
        //更新品牌表
        boolean b = updateById(brand);
        //更新品牌表失败
        if (!b){
            throw  new LyException(500,"更新失败");
        }
        //删除中间表数据
        boolean remove = categoryBrandService.remove(
                new QueryWrapper<CategoryBrand>().eq("brand_id", brandDTO.getId()));
        //删除失败
        if (!remove){
            throw new LyException(500,"中间表跟新失败");
        }
        //重新添加
        List<CategoryBrand> list = brandDTO.getCategoryIds().stream().map(id -> CategoryBrand.of(id, brandDTO.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(list);
    }
}
